package com.ywu.bpmn.form.service.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.bpmn.form.service.data.YwuFormDefinitionRepository;
import com.ywu.bpmn.form.service.data.YwuFormInterfaceRepository;
import com.ywu.bpmn.form.service.dto.YwuFormDefinitionDto;
import com.ywu.bpmn.form.service.dto.YwuFormDefinitionVersionDto;
import com.ywu.bpmn.form.service.entity.YwuFormDefinitionEntity;
import com.ywu.bpmn.form.service.entity.YwuFormDefinitionVersionEntity;
import com.ywu.bpmn.form.service.entity.YwuFormInterfaceEntity;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName YwuFormDefinitionService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/20 15:44
 * @Version 1.0
 **/
@Service
public class YwuFormDefinitionService implements ApplicationContextAware {
    @Autowired
    private YwuFormDefinitionRepository repository;

    @Autowired
    private YwuFormInterfaceRepository interfaceRepository;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
    * @Author GroundDemo
    * @Description 创建表单
    * @Date 15:46 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> createDefinition(YwuFormDefinitionDto dto) {
        // 校验编码是否存在
        YwuFormDefinitionEntity search = new YwuFormDefinitionEntity();
        search.setCode(dto.getCode());
        YwuFormDefinitionEntity queryRes = repository.getEntityNoJson(search);
        if (Objects.nonNull(queryRes)) {
            // 编码已存在
            return CommonResult.error().setErrorCode("form.code.has.exist");
        }
        // 构建插入对象
        YwuFormDefinitionEntity insert = new YwuFormDefinitionEntity();
        BeanUtils.copyProperties(dto, insert);
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        insert.setId(TimerIdGenerateUtil.nextId());
        insert.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        insert.createInfo(currentContext.getYwuContextUser().getUserAccount());
        insert.setFormJson("{\"globalProperties\": {\"colNum\": 1}, \"componentJson\": []}");
        // 创建的初始状态为草稿
        insert.setStatus(YwuFormDefinitionEntity.DRAFT_STATUS);
        insert.setCurrentVersion(0);
        insert.setHasNotReleased(0);
        repository.insert(insert);
        return CommonResult.success(insert.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 根据当前租户ID查询数据
    * @Date 15:58 2025/4/20
    * @Param [dto, isQueryJson]
    * @return com.ywu.bpmn.form.service.entity.YwuFormDefinitionEntity
    **/
    private YwuFormDefinitionEntity queryEntityByTenantId(YwuFormDefinitionDto dto, boolean isQueryJson) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFormDefinitionEntity search = new YwuFormDefinitionEntity();
        search.setId(dto.getId());
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        YwuFormDefinitionEntity queryRes;
        if (isQueryJson) {
            queryRes = repository.getEntity(search);
        } else {
            queryRes = repository.getEntityNoJson(search);
        }
        if (Objects.isNull(queryRes)) {
            return null;
        } else {
            return queryRes;
        }
    }

    /**
    * @Author GroundDemo
    * @Description 修改表单定义
    * @Date 15:57 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateDefinition(YwuFormDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(dto, true);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 如果当前数据是草稿，则直接修改当前数据即可
        if (queryRes.getStatus().equals(YwuFormDefinitionEntity.DRAFT_STATUS)) {
            YwuFormDefinitionEntity update = new YwuFormDefinitionEntity();
            BeanUtils.copyProperties(dto, update);
            update.setHasNotReleased(queryRes.getHasNotReleased());
            update.setCurrentVersion(queryRes.getCurrentVersion());
            update.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            update.setStatus(queryRes.getStatus());
            repository.update(update);
        } else {
            // 如果当前版本是已发布的，则需要去version表中插入一条版本为0的草稿数据，并将当前数据的HasNotReleased状态置为1
            // 构建草稿版本数据
            YwuFormDefinitionVersionEntity insertVersion = new YwuFormDefinitionVersionEntity();
            BeanUtils.copyProperties(dto, insertVersion);
            insertVersion.setId(TimerIdGenerateUtil.nextId());
            insertVersion.setResourceId(queryRes.getId());
            insertVersion.setVersion(0);
            insertVersion.createInfo(currentContext.getYwuContextUser().getUserAccount());
            if (queryRes.getHasNotReleased() == 1) {
                // 已经存在草稿版本，需要查询出草稿版本的数据
                YwuFormDefinitionVersionEntity searchVersionEntity = new YwuFormDefinitionVersionEntity();
                searchVersionEntity.setResourceId(queryRes.getId());
                searchVersionEntity.setVersion(0);
                YwuFormDefinitionVersionEntity versionEntity = repository.getVersionEntity(searchVersionEntity);
                insertVersion.setFormJson(versionEntity.getFormJson());
            } else {
                insertVersion.setFormJson(queryRes.getFormJson());
            }
            insertVersion.setTenantId(currentContext.getYwuContextTenant().getTenantId());
            // 采用先删除原始草稿版本的数据，再插入新的草稿数据
            YwuFormDefinitionVersionEntity deleteVersion = new YwuFormDefinitionVersionEntity();
            deleteVersion.setResourceId(queryRes.getId());
            deleteVersion.setVersion(0);
            repository.deleteVersionByVersion(deleteVersion);
            repository.insertVersion(insertVersion);
            queryRes.setHasNotReleased(1);
            queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.update(queryRes);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新表单JSON
    * @Date 15:57 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateFormJson(YwuFormDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(dto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 如果当前数据是草稿，则直接修改当前数据即可
        if (queryRes.getStatus().equals(YwuFormDefinitionEntity.DRAFT_STATUS)) {
            YwuFormDefinitionEntity update = new YwuFormDefinitionEntity();
            update.setFormJson(dto.getFormJson());
            update.setId(dto.getId());
            update.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.updateFormJson(update);
        } else {
            // 如果当前版本是已发布的，则需要去version表中插入一条版本为0的草稿数据，并将当前数据的HasNotReleased状态置为1
            YwuFormDefinitionVersionEntity insertVersion = new YwuFormDefinitionVersionEntity();
            if (queryRes.getHasNotReleased() == 1) {
                // 已经存在草稿版本，需要查询出草稿版本的数据
                YwuFormDefinitionVersionEntity searchVersionEntity = new YwuFormDefinitionVersionEntity();
                searchVersionEntity.setResourceId(queryRes.getId());
                searchVersionEntity.setVersion(0);
                YwuFormDefinitionVersionEntity versionEntity = repository.getVersionEntity(searchVersionEntity);
                BeanUtils.copyProperties(versionEntity, insertVersion);
            } else {
                BeanUtils.copyProperties(queryRes, insertVersion);
            }
            insertVersion.setId(TimerIdGenerateUtil.nextId());
            insertVersion.setVersion(0);
            insertVersion.setResourceId(queryRes.getId());
            insertVersion.createInfo(currentContext.getYwuContextUser().getUserAccount());
            insertVersion.setFormJson(dto.getFormJson());
            // 采用先删除原始草稿版本的数据，再插入新的草稿数据
            YwuFormDefinitionVersionEntity deleteVersion = new YwuFormDefinitionVersionEntity();
            deleteVersion.setVersion(0);
            deleteVersion.setResourceId(queryRes.getId());
            repository.deleteVersionByVersion(deleteVersion);
            repository.insertVersion(insertVersion);
            queryRes.setHasNotReleased(1);
            queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.update(queryRes);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除表单
    * @Date 16:19 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteFormDefinition(YwuFormDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(dto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 删除数据
        YwuFormDefinitionEntity delete = new YwuFormDefinitionEntity();
        delete.setId(dto.getId());
        repository.delete(delete);
        // 异步删除所有历史版本
        YwuFormDefinitionService appService = applicationContext.getBean(YwuFormDefinitionService.class);
        appService.deleteFromOtherInfoAsync(dto);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除其他表单相关信息 历史版本、外部接口等
    * @Date 16:23 2025/4/20
    * @Param [dto]
    * @return void
    **/
    @Async
    public void deleteFromOtherInfoAsync(YwuFormDefinitionDto dto) {
        // 删除所有表单历史版本信息
        YwuFormDefinitionVersionEntity deleteVersion = new YwuFormDefinitionVersionEntity();
        deleteVersion.setResourceId(dto.getId());
        repository.deleteVersionByVersion(deleteVersion);
        // 删除外部接口相关信息
        YwuFormInterfaceEntity deleteInterface = new YwuFormInterfaceEntity();
        deleteInterface.setFormId(dto.getId());
        interfaceRepository.deleteByFormId(deleteInterface);
    }

    /**
    * @Author GroundDemo
    * @Description 查询表单分页列表
    * @Date 16:25 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryPageList(YwuFormDefinitionDto dto) {
        // 构建分页
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        PageService.execPageStartAndEnd(dto.getPageInfo());
        dto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        // 查询所有数量
        int count = repository.queryCount(dto);
        if (count == 0) {
            PageList<YwuFormDefinitionEntity> pageList = new PageList<>(dto.getPageInfo().getPageIndex(),
                    dto.getPageInfo().getPageSize(), count, new ArrayList<>());
            return CommonResult.success(pageList);
        }
        // 查询所有数据
        List<YwuFormDefinitionEntity> queryList = repository.queryPageList(dto);
        // 批量查询需要填充草稿版本的信息
        List<String> ids = queryList.stream().filter(res ->
                res.getStatus().equals(YwuFormDefinitionEntity.ONLINE_STATUS) && res.getHasNotReleased() == 1)
                .map(YwuFormDefinitionEntity::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(ids)) {
            // 批量查询草稿版本
            List<YwuFormDefinitionVersionEntity> draftVersionList = repository.queryDraftVersionList(ids);
            // 构建map，方便后续填充
            Map<String, YwuFormDefinitionVersionEntity> draftMap = draftVersionList.stream()
                    .collect(Collectors.toMap(YwuFormDefinitionVersionEntity::getResourceId,
                            res -> res, (v1, v2) -> v1));
            // 填充数据
            for (YwuFormDefinitionEntity definitionEntity : queryList) {
                if (draftMap.containsKey(definitionEntity.getId())) {
                    YwuFormDefinitionVersionEntity draftInfo = draftMap.get(definitionEntity.getId());
                    BeanUtils.copyProperties(draftInfo, definitionEntity, "id", "createBy", "createDate", "updateBy", "updateDate");
                    definitionEntity.setId(draftInfo.getResourceId());
                }
            }
        }
        PageList<YwuFormDefinitionEntity> pageList = new PageList<>(dto.getPageInfo().getPageIndex(),
                dto.getPageInfo().getPageSize(), count, queryList);
        return CommonResult.success(pageList);
    }

    /**
    * @Author GroundDemo
    * @Description 查询表单信息
    * @Date 16:31 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryFormEntity(YwuFormDefinitionDto dto) {
        YwuFormDefinitionEntity search = new YwuFormDefinitionEntity();
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        BeanUtils.copyProperties(dto, search);
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        YwuFormDefinitionEntity entity = repository.getEntity(search);
        if (Objects.isNull(entity)) {
            return CommonResult.error().setErrorCode("query.info.not.exist");
        }
        // 判断当前是否存在草稿版本，如果存在草稿版本，则查询草稿并返回
        if (entity.getStatus().equals(YwuFormDefinitionEntity.ONLINE_STATUS) && entity.getHasNotReleased() == 1) {
            YwuFormDefinitionVersionEntity searchDraft = new YwuFormDefinitionVersionEntity();
            searchDraft.setVersion(0);
            searchDraft.setResourceId(entity.getId());
            YwuFormDefinitionVersionEntity versionEntity = repository.getVersionEntity(searchDraft);
            // 重新构建对象
            BeanUtils.copyProperties(versionEntity, entity, "id", "createBy", "createDate", "updateBy", "updateDate");
            entity.setId(versionEntity.getResourceId());
        }
        return CommonResult.success(entity);
    }

    /**
    * @Author GroundDemo
    * @Description 表单发布
    * @Date 16:55 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> publishForm(YwuFormDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(dto, true);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 如果当前状态时草稿，则需要将当前数据的状态改为已发布
        if (queryRes.getStatus().equals(YwuFormDefinitionEntity.DRAFT_STATUS)) {
            YwuFormDefinitionEntity update = new YwuFormDefinitionEntity();
            BeanUtils.copyProperties(queryRes, update);
            update.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            update.setStatus(YwuFormDefinitionEntity.ONLINE_STATUS);
            update.setHasNotReleased(0);
            update.setCurrentVersion(queryRes.getCurrentVersion() + 1);
            repository.update(update);
            // 添加一条版本记录
            YwuFormDefinitionVersionEntity insertVersion = new YwuFormDefinitionVersionEntity();
            BeanUtils.copyProperties(queryRes, insertVersion);
            insertVersion.setId(TimerIdGenerateUtil.nextId());
            insertVersion.createInfo(currentContext.getYwuContextUser().getUserAccount());
            insertVersion.setResourceId(queryRes.getId());
            insertVersion.setVersion(queryRes.getCurrentVersion() + 1);
            repository.insertVersion(insertVersion);
        } else {
            // 当前状态为已发布，需要读取草稿版本的数据，将其数据填充到原始表中
            YwuFormDefinitionVersionEntity searchEntity = new YwuFormDefinitionVersionEntity();
            searchEntity.setResourceId(queryRes.getId());
            searchEntity.setVersion(0);
            YwuFormDefinitionVersionEntity versionEntity = repository.getVersionEntity(searchEntity);
            if (Objects.isNull(versionEntity)) {
                // 不存在草稿版本，无法发布
                return CommonResult.error().setErrorCode("draft.version.is.not.exist");
            }
            YwuFormDefinitionEntity newFormEntity = new YwuFormDefinitionEntity();
            BeanUtils.copyProperties(queryRes, newFormEntity);
            BeanUtils.copyProperties(versionEntity, newFormEntity);
            newFormEntity.setFormJson(versionEntity.getFormJson());
            newFormEntity.setId(queryRes.getId());
            newFormEntity.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            newFormEntity.setCurrentVersion(queryRes.getCurrentVersion() + 1);
            newFormEntity.setHasNotReleased(0);
            repository.updateAll(newFormEntity);
            // 将原始那条草稿版本的数据的版本号改为最新的版本号
            versionEntity.setVersion(queryRes.getCurrentVersion() + 1);
            repository.updateVersionById(versionEntity);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 根据表单ID查询所有历史版本信息
    * @Date 16:33 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryFormVersionList(YwuFormDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(dto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 根据resourceId查询所有历史版本
        YwuFormDefinitionVersionDto search = new YwuFormDefinitionVersionDto();
        search.setResourceId(dto.getId());
        List<YwuFormDefinitionVersionEntity> searchList = repository.queryVersionList(search);
        if (CollectionUtils.isEmpty(searchList)) {
            return CommonResult.success(searchList);
        }
        // 如果最后一个元素是 0 版本，则提到最前面
        if (searchList.get(searchList.size() - 1).getVersion() == 0) {
            YwuFormDefinitionVersionEntity draftVersionEntity = searchList.get(searchList.size() - 1);
            searchList.remove(searchList.size() - 1);
            List<YwuFormDefinitionVersionEntity> newList = new ArrayList<>();
            newList.add(draftVersionEntity);
            newList.addAll(searchList);
            searchList = newList;
        }
        return CommonResult.success(searchList);
    }

    /**
    * @Author GroundDemo
    * @Description 查询历史版本详情
    * @Date 16:37 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryVersionEntity(YwuFormDefinitionVersionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionDto searchDto = new YwuFormDefinitionDto();
        searchDto.setId(dto.getResourceId());
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(searchDto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("resource.id.is.not.exist");
        }
        // 查询版本信息
        YwuFormDefinitionVersionEntity searchEntity = new YwuFormDefinitionVersionEntity();
        BeanUtils.copyProperties(dto, searchEntity);
        YwuFormDefinitionVersionEntity versionEntity = repository.getVersionEntity(searchEntity);
        return CommonResult.success(versionEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 删除历史版本
    * @Date 16:39 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> deleteVersion(YwuFormDefinitionVersionDto dto) {
        if (dto.getVersion() != 0) {
            return CommonResult.error().setErrorCode("version.can.not.delete");
        }
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionDto searchDto = new YwuFormDefinitionDto();
        searchDto.setId(dto.getResourceId());
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(searchDto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("resource.id.is.not.exist");
        }
        // 删除数据
        YwuFormDefinitionVersionEntity delete = new YwuFormDefinitionVersionEntity();
        delete.setResourceId(dto.getResourceId());
        delete.setVersion(dto.getVersion());
        repository.deleteVersionByVersion(delete);
        // 修改原始数据的hasNotReleased状态为0
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        queryRes.setHasNotReleased(0);
        queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
        repository.update(queryRes);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 回退版本
    * @Date 16:43 2025/4/20
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> backVersion(YwuFormDefinitionVersionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFormDefinitionDto searchDto = new YwuFormDefinitionDto();
        searchDto.setId(dto.getResourceId());
        YwuFormDefinitionEntity queryRes = queryEntityByTenantId(searchDto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("resource.id.is.not.exist");
        }
        if (queryRes.getStatus().equals(YwuFormDefinitionEntity.DRAFT_STATUS)) {
            // 当前操作状态，无法回退
            return CommonResult.error().setErrorCode("draft.status.can.not.back");
        }
        // 查询待回退的历史版本数据
        YwuFormDefinitionVersionEntity search = new YwuFormDefinitionVersionEntity();
        search.setResourceId(dto.getResourceId());
        search.setVersion(dto.getVersion());
        YwuFormDefinitionVersionEntity versionEntity = repository.getVersionEntity(search);
        if (Objects.isNull(versionEntity)) {
            return CommonResult.error().setErrorCode("version.is.not.exist");
        }
        // 删除草稿版本的数据（如果有的话）
        YwuFormDefinitionVersionEntity deleteDraft = new YwuFormDefinitionVersionEntity();
        deleteDraft.setVersion(0);
        deleteDraft.setResourceId(dto.getResourceId());
        repository.deleteVersionByVersion(deleteDraft);
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 添加一条新的草稿数据，数据来源就是之前查询的历史版本的数据
        YwuFormDefinitionVersionEntity insert = new YwuFormDefinitionVersionEntity();
        BeanUtils.copyProperties(versionEntity, insert);
        insert.setId(TimerIdGenerateUtil.nextId());
        insert.setVersion(0);
        insert.createInfo(currentContext.getYwuContextUser().getUserAccount());
        repository.insertVersion(insert);
        // 修改原始表中数据hasNotReleased状态为1
        queryRes.setHasNotReleased(1);
        queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
        repository.update(queryRes);
        return CommonResult.success(null);
    }
}
